package com.hmall.trade.Listener;

import com.hmall.trade.domain.po.Order;
import com.hmall.trade.service.IOrderDetailService;
import com.hmall.trade.service.IOrderService;
import lombok.RequiredArgsConstructor;
import org.springframework.amqp.rabbit.annotation.Exchange;
import org.springframework.amqp.rabbit.annotation.Queue;
import org.springframework.amqp.rabbit.annotation.QueueBinding;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

/**
 * CLassName: PayStatusListener
 * Description:
 * Author: wuzhixiang
 * Date: 2024-11-23 20:50
 * Version: 1.0
 */
@Component
@RequiredArgsConstructor
public class PayStatusListener {

    private final IOrderService orderService;

    @RabbitListener(bindings = @QueueBinding(
            value = @Queue(name = "trade.pay.success.queue",declare = "true"),
            exchange = @Exchange(name = "pay.direct"),
            key = "pay.success"
    ))
    public void listenPaySuccess(Long orderId){
        //1 查询订单
        Order order = orderService.getById(orderId);
        //2 判断订单状态,是否未支付
        if(order == null || order.getStatus() != 1){
            return;
        }
        //标记订单为已支付
        orderService.markOrderPaySuccess(orderId);
    }

}
